/**
 * 
 */
package com.hujun.common.time.core.impl;

import java.text.ParseException;
import java.util.Map;

import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hujun.common.time.common.TimerConstant;
import com.hujun.common.time.config.CronTriggerConfig;
import com.hujun.common.time.config.JobConfig;
import com.hujun.common.time.config.SchedulerConfig;
import com.hujun.common.time.config.SimpleTriggerConfig;
import com.hujun.common.time.config.TriggerConfig;
import com.hujun.common.time.core.ConfigFactory;
import com.hujun.common.time.core.SchedulingJobService;
import com.hujun.common.time.listener.TimerJobListener;

/**
 * @author Administrator
 *
 */
public class SchedulingJobServiceImpl implements SchedulingJobService{
	private static Scheduler scheduler;
	private static final Logger log = LoggerFactory.getLogger(SchedulingJobServiceImpl.class);
	public Scheduler getScheduler() throws SchedulerException {
		if(null == scheduler){
			synchronized (SchedulingJobServiceImpl.class) {
				if(null == scheduler){
					 SchedulerFactory schedulerFactory = new StdSchedulerFactory();
					 try {
						scheduler = schedulerFactory.getScheduler();
						scheduler.addGlobalJobListener(new TimerJobListener());
					} catch (SchedulerException e) {
						log.error("schedulerFactory.getScheduler() error! ");
						throw e;
					}
				}
			}
		}
		return scheduler;
	}

	public void scheduleAllJob() {
		SchedulerConfig config = null;
		try {
			config = ConfigFactory.getConfig();
		} catch (Exception e) {
			log.error("ConfigFactory.getConfig() error!");
			throw new IllegalArgumentException();
		}
		if(config != null){
			Map<String,JobConfig> jobs = config.getJobs();
			for(String jobName : jobs.keySet()){
				JobDetail jobDetail = new JobDetail();
				jobDetail.setName(jobName);
				try {
					jobDetail.setJobClass(Class.forName(jobs.get(jobName).getClassName()).asSubclass(Job.class));
				} catch (ClassNotFoundException e) {
					log.error("Class.forName(jobs.get(jobName).getClassName()).asSubclass(Job.class) error!");
				}
				TriggerConfig tc = config.getTriggers().get(jobs.get(jobName).getTriggerName());
				if(tc.getType().equals(TimerConstant.TRIGGER_SIMPLE)){
					SimpleTriggerConfig stc  = (SimpleTriggerConfig)tc;
					SimpleTrigger st = new SimpleTrigger(stc.getTriggerName(), stc.getStartDate(), stc.getEndDate(),
							stc.getRepeatCount(), stc.getRepeatInterval());
					try {
						getScheduler().scheduleJob(jobDetail, st);
					} catch (SchedulerException e) {
						log.error("getScheduler().scheduleJob(jobDetail, st);");
					}
				}else if(tc.getType().equals(TimerConstant.TRIGGER_CRON)){
					CronTriggerConfig ctc = (CronTriggerConfig)tc;
					CronTrigger ct = new CronTrigger(ctc.getTriggerName());
					try {
						ct.setCronExpression(ctc.getCronExpression());
					} catch (ParseException e) {
						log.error("set CronExpression error!");
					}
					try {
						getScheduler().scheduleJob(jobDetail, ct);
					} catch (SchedulerException e) {
						log.error("getScheduler().scheduleJob(jobDetail, ct) error!");
					}
				}
			}
		}
		
	}

	public void reSchedule() {
		// TODO Auto-generated method stub
		
	}

}
